package org.sunrise.game.game.logic.system;

import com.alibaba.fastjson2.TypeReference;
import org.sunrise.game.game.human.HumanObject;
import org.sunrise.game.game.modules.MinerModule;

import java.util.HashMap;
import java.util.LinkedHashMap;

public class MinerSystem extends BaseSystem {
    private HashMap<String, Integer> ranks = new HashMap<>();
    @Override
    public void load() {
        getDbData("ranks", new TypeReference<HashMap<String, Integer>>() {
        }, value -> ranks = value);
    }

    @Override
    public void save() {
        putDbData("ranks", ranks);
    }

    public void update(HumanObject humanObject) {
        ranks.put(humanObject.getConnectObject().getUid(), humanObject.getModule(MinerModule.class).getLevelIndex());
        ranks = ranks.entrySet()
                .stream()
                .sorted((a, b) -> b.getValue().compareTo(a.getValue())) // 降序
                .collect(
                        LinkedHashMap::new,
                        (map, entry) -> map.put(entry.getKey(), entry.getValue()),
                        LinkedHashMap::putAll
                );
    }

    public HashMap<String, Integer> getInfo() {
        return ranks;
    }
}
